package com.zhuliwei.controller;

import com.alibaba.excel.util.StringUtils;
import com.zhuliwei.domain.ResponseResult;
import com.zhuliwei.domain.dto.Captcha;
import com.zhuliwei.domain.entity.LoginUser;
import com.zhuliwei.domain.entity.Menu;
import com.zhuliwei.domain.entity.User;
import com.zhuliwei.domain.vo.AdminUserInfoVo;
import com.zhuliwei.domain.vo.RoutersVo;
import com.zhuliwei.domain.vo.UserInfoVo;
import com.zhuliwei.enums.AppHttpCodeEnum;
import com.zhuliwei.exception.SystemException;
import com.zhuliwei.service.AdminAuthService;
import com.zhuliwei.service.MenuService;
import com.zhuliwei.service.RoleService;
import com.zhuliwei.service.impl.CaptchaServiceImpl;
import com.zhuliwei.utils.BeanCopyUtils;
import com.zhuliwei.utils.SecurityUtils;
import io.swagger.annotations.Api;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 后台认证控制层
 *
 * @author 朱力维
 * @time 2022/10/25 15:30
 **/
@RestController
@Api(tags="认证",description = "后台认证相关接口")
public class AdminAuthController {
    @Resource
    AdminAuthService adminAuthService;

    @Resource
    RoleService roleService;

    @Resource
    MenuService menuService;

    @Resource
    CaptchaServiceImpl captchaService;

    @PostMapping("get-captcha")
    public ResponseResult getCaptcha(@RequestBody Captcha captcha) {
        return ResponseResult.okResult(captchaService.getCaptcha(captcha));
    }

    @PostMapping("/user/login")
    public ResponseResult login(@RequestBody User user, HttpServletRequest request) {
        //先校验验证码
//        String msg = captchaService.checkImageCode(user.getNonceStr(),user.getValue());
//        if (StringUtils.isNotBlank(msg)) {
//            return ResponseResult.errorResult(500,msg);
//        }
        if (StringUtils.isBlank(user.getUserName())) {
            //提示 必须要传用户名
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        } else if (StringUtils.isBlank(user.getPassword())) {
            throw new SystemException(AppHttpCodeEnum.REQUIRE_PASSWORD);
        }
        return adminAuthService.login(user,request);
    }

    @PostMapping("/user/logout")
    public ResponseResult logout() {
        return adminAuthService.logout();
    }

    @GetMapping("/getInfo")
    public ResponseResult<AdminUserInfoVo> getInfo() {
        //获取当前登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //根据用户id查询权限信息
        List<String> perms = menuService.selectPermsByUserId(loginUser.getUser().getId());
        //根据用户id查询角色信息
        List<String> roleKeyList = roleService.selectRoleKeyByUserId(loginUser.getUser().getId());
        //获取用户信息
        User user = loginUser.getUser();
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user,UserInfoVo.class);
        //封装数据返回
        AdminUserInfoVo adminUserInfoVo = new AdminUserInfoVo(perms,roleKeyList,userInfoVo);
        return ResponseResult.okResult(adminUserInfoVo);
    }

    @GetMapping("/getRouters")
    public ResponseResult<RoutersVo> getRouters(){
        String userId = SecurityUtils.getUserId();
        //查询menu 结果是tree的形式
       List<Menu> menus = menuService.selectRouterMenuTreeByUserId(userId);
        //封装数据返回
        return ResponseResult.okResult(new RoutersVo(menus));
    }

}
